package com.song.estore.action;

import com.song.estore.domain.Product;
import com.song.estore.service.ProductService;
import com.song.estore.utils.FileDirUtils;
import com.song.estore.utils.PicUtils;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * Created by 范姣姣 on 2015/10/22.
 */
public class AddProductAction {

    private String name;
    private Double marketprice;
    private Double estoreprice;
    private String category;
    private int pnum;
    private File product_img;
    private String product_imgFileName;
    private String description;


    private ProductService productService;
    private String msg;

    String saveurl = "";

    public String execute() {
        try {

            String uuidFileName = UUID.randomUUID().toString().replaceAll("-", "");
            uuidFileName = uuidFileName + product_imgFileName.substring(product_imgFileName.lastIndexOf("."));
            String fileDir = FileDirUtils.getDir(product_imgFileName);

            String path = ServletActionContext.getServletContext().getRealPath("/upload") + fileDir;
            File file = new File(path);
            if (!file.exists()) {
                file.mkdirs();
            }
            FileUtils.copyFile(product_img, new File(file, uuidFileName));
            PicUtils picUtils = null;
            picUtils = new PicUtils(path + "/" + uuidFileName);
            picUtils.resize(100, 100);

            saveurl = "/upload" + fileDir + "/" + uuidFileName;

        } catch (IOException e) {
            e.printStackTrace();
        }


        Product pro = new Product(name, category, marketprice, estoreprice, pnum, saveurl, description);

        productService.addProduct(pro);

        msg = "《" + pro.getName() + "》添加成功";

        return "success";

    }

    public String getProduct_imgFileName() {
        return product_imgFileName;
    }

    public void setProduct_imgFileName(String product_imgFileName) {
        this.product_imgFileName = product_imgFileName;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Double getMarketprice() {
        return marketprice;
    }

    public void setMarketprice(Double marketprice) {
        this.marketprice = marketprice;
    }

    public Double getEstoreprice() {
        return estoreprice;
    }

    public void setEstoreprice(Double estoreprice) {
        this.estoreprice = estoreprice;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public int getPnum() {
        return pnum;
    }

    public void setPnum(int pnum) {
        this.pnum = pnum;
    }

    public File getProduct_img() {
        return product_img;
    }

    public void setProduct_img(File product_img) {
        this.product_img = product_img;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public void setProductService(ProductService productService) {

        this.productService = productService;
    }
}
